<template>
  <root-portal>
    <view
      class="privacy"
      v-if="showPrivacy"
    >
      <view class="privacy-dialog">
        <view class="privacy-header">隐私保护指引</view>
        <view class="privacy-body">
          在使用当前小程序服务之前，请仔细阅读
          <text
            class="link"
            @click="showPrivacyContract()"
            >{{ privacyContractName }}</text
          >
          。如果你同意<text
            class="link"
            @click="showPrivacyContract()"
            >{{ privacyContractName }}</text
          >，请点击“同意”开始使用。
        </view>

        <view class="privacy-footer">
          <btn>
            <button @click="exitMiniProgram">拒绝</button>
          </btn>
          <btn
            id="agree-btn"
            type="primary"
          >
            <button
              open-type="agreePrivacyAuthorization"
              @agreeprivacyauthorization="handleAgreePrivacyAuthorization"
            >
              同意
            </button>
          </btn>
        </view>
      </view>
    </view></root-portal
  >
</template>

<script>
  export default {
    name: "wxPrivacy",
    data() {
      return {
        privacyContractName: "",
        showPrivacy: false,
      };
    },
    created() {
      setTimeout(() => {
        this.showPrivacy = getApp().globalData.showPrivacy;
        this.privacyContractName = getApp().globalData.privacyContractName;
      }, 500);
    },
    methods: {},
    mounted() {},

    methods: {
      // 同意微信小程序隐私保护指引
      handleAgreePrivacyAuthorization() {
        const that = this;
        wx.requirePrivacyAuthorize({
          success: (res) => {
            that.showPrivacy = false;
            getApp().globalData.showPrivacy = false;
          },
        });
      },

      // 拒绝微信小程序隐私保护指引
      exitMiniProgram() {
        const that = this;
        uni.showModal({
          content:
            "如果拒绝,我们将无法获取您的信息, 包括手机号、位置信息、相册等该小程序十分重要的功能,您确定要拒绝吗?",
          success: (res) => {
            if (res.confirm) {
              that.showPrivacy = false;
              uni.exitMiniProgram({
                success: () => {
                  console.log("退出小程序成功");
                },
              });
            }
          },
        });
      },

      // 展示微信小程序隐私保护指引 微信封装好的不用进行其他开发
      showPrivacyContract() {
        wx.openPrivacyContract({
          fail: () => {
            uni.showToast({
              title: "网络错误",
              icon: "error",
            });
          },
        });
      },
    },
  };
</script>

<style lang="scss" scoped>
  view,
  text {
    font-size: 28rpx;
  }
  .privacy {
    position: fixed;
    z-index: 9999;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    &-dialog {
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      padding: 40rpx;
      padding-bottom: calc(20rpx + constant(safe-area-inset-bottom));
      padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
      background: #fff;
      border-radius: 20rpx;
    }

    &-header {
      font-size: 36rpx;
      font-weight: bold;
      text-align: center;
      margin-bottom: 20rpx;
    }

    &-body {
      font-size: 28rpx;
      color: #999;
      margin-bottom: 20rpx;
      .link {
        color: $color-primary;
      }
    }

    &-footer {
      display: flex;
      justify-content: space-between;
      align-items: center;
      button {
      }
    }
  }
</style>
